package com.qf.day07.poly;

import com.qf.day06.Animal;
import com.qf.day06.Dog;
import com.qf.day06.Fish;

/**
 * 演示 instancof关键字的用法
 */
public class InstanceOfDemo {
    public static void main(String[] args) {

        Dog dog = new Dog();
        boolean b = dog instanceof Dog;
        boolean b1 = dog instanceof Animal;
        boolean b2 = dog instanceof Object;
        System.out.println(b);
        System.out.println(b1);
        System.out.println(b2);
        System.out.println("===========");
        Animal animal = new Animal();
        boolean b3 = animal instanceof Animal;
        boolean b4 = animal instanceof Object;
        System.out.println(b3);
        System.out.println(b4);
        boolean b5 = animal instanceof Dog;
        System.out.println(b5);
        System.out.println("===========");
        Fish fish = new Fish();
        boolean b6 = fish instanceof Fish;
        System.out.println(b6);

        System.out.println("===============");
//        Animal animal1 = new Dog();
//        Animal animal2 = new Fish();
        Animal animal1 = new Fish();
        // 如何避免出现类型转换的运行时异常
        if (animal1 instanceof Dog){
            Dog dog1 = (Dog) animal1;
            dog1.lookHouse();
        }else {
            System.out.println("不是Dog");
        }

    }
}
